package com.neuedu.javase.io;

import java.io.*;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-05 11:33
 * 描述 : 字符流
 */
public class MyCharSteam {

    public static void main(String[] args) throws Exception {

        //                readOneChar();


        //                readCharMore();

        //读取所有字符
        //                readAllChars();


        /**
         * 输出字符
         */
//        write();


        //文件的复制
        String src = "d:\\io\\a.txt";
        String dest = "d:\\io\\b.txt";
        boolean success = copy(src,dest);
        System.out.println("success = " + success);


    }


    /**
     * 读取文件内容  读一个字符
     */
    static void readOneChar() {

        File file = new File("D:\\io\\a.txt");
        Reader reader = null;
        //ALT +ENTER 提示
        try {
            reader = new FileReader(file);

            int oneChar = reader.read();

            System.out.println("oneByte = " + (char) oneChar);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }


    /**
     * 读取文件内容  读多个字符
     */
    static void readCharMore() {

        File file = new File("D:\\io\\a.txt");
        Reader reader = null;
        //ALT +ENTER 提示
        try {
            reader = new FileReader(file);

            char[] chars = new char[5];

            //尽量读5个字符
            int len = reader.read(chars);

            System.out.println("len = " + len);
            System.out.println("chars = " + new String(chars));


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }


    /**
     * 读取文件内容  读所有字节
     */
    static void readAllChars() {

        File file = new File("D:\\io\\a.txt");
        Reader reader = null;
        //ALT +ENTER 提示
        try {
            reader = new FileReader(file);

            char[] chars = new char[1024];

            //            reader.reset();

            //            while(true) {
            //                //每次尽量读5个字节
            //                int len = reader.read(chars);
            //
            //                System.out.println("len = " + len);
            //                System.out.println("chars = " + new String(chars));
            //
            //                if(len == -1){
            //                    break;
            //                }
            //            }

            int len = -1;
            while ((len = reader.read(chars)) != -1) {
                System.out.println("len = " + len);
                System.out.println("chars = " + new String(chars, 0, len));
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }

    }


    static void write() {
        String fileName = "D:\\io\\a.txt";
        Writer writer = null;
        try {
            //            os = new FileOutputStream(fileName); //默认是覆盖
            writer = new FileWriter(fileName, true); //追加写入


            //写入 字符  A
            //            writer.write('A');

            //写入一个字节数组
            //           char[] chs = new char[]{};
//            char[] chs = {'H','E','L','L','O'};
//            writer.write(chs);

//            String str = " i am string";
//            writer.write(str);


            char[] chs = {'H','E','L','L','O'};
            writer.write(chs,0,5);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }





    static boolean copy(String src, String dest) throws Exception {
        return copy(new File(src), new File(dest));
    }

    static boolean copy(File src, File dest) throws Exception {

        if (!src.exists()) {
            throw new Exception("[" + src.getPath() + "]文件不存在");
        }

        BufferedReader reader = null;
        Writer writer = null;


        try {


            //读写内容
            reader = new BufferedReader(new FileReader(src));
            writer = new BufferedWriter(new FileWriter(dest));

//            writer.append("null").append(null);


            String line  = null;

            while ((line = reader.readLine()) != null) {
                //              bts

                writer.write(line+"\r\n");
                writer.flush();//刷新缓冲区
            }


            return true;
        } catch (Exception e) {

        } finally {
            //关闭资源
            if (reader != null) {
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }

        }


        return false;

    }


}
